<?php

namespace App\Http\Controllers;

use App\Models\User;
use Cookie;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function getUserLdSpread($uid)
    {
        $user = User::where('id',$uid)->first();

        if($user['pid']){
            //上级
            $boss = $this->getBossUid($user['pid'],$uid);

            $boss_user = User::where('id',$boss['boss_id'])->first();

            if($boss['boss_id']){
                $agent_list = User::where('pid', $boss['boss_id'])->where('identity',10)->limit(2)
                    ->orderBy('up_agent_time','asc')->get();
                if(count($agent_list) >= 2){
                    $agent_ids = [];
                    foreach ($agent_list as $value){
                        $agent_ids[] = $value['id'];
                    }
                    if(in_array($boss['ouid'],$agent_ids)){
                        if($boss_user['pid']){
                            $jd = $this->getBossUid($boss_user['pid'],$boss['boss_id']);
                            return $jd['boss_id'];
                        }else{
                            return 0;
                        }
                    }else{
                        return $boss['boss_id'];
                    }
                }else{
                    return $this->getUserLdSpread($boss_user['id']);
                }

            }else{
                return 0;
            }
        }else{
            return 0;
        }
    }
    public function getBossUid($uid,$ouid)
    {
        $user = User::where('id',$uid)->first();
        if($user['identity'] == 20){
            return ['boss_id' => $uid,'ouid' => $ouid];
        }else{
            if($user['pid']){
                return self::getBossUid($user['pid'],$uid);
            }else{
                return ['boss_id' => 0,'ouid' => $ouid];
            }
        }
    }

    public function getParentsByUserId($userId, &$users = [])
    {
        $userInfo = User::where('id',$userId)->first();
        if (empty($userInfo)) {
            return $users;
        }
        $users[] = $userInfo;
        if ($userInfo['pid'] == 0) {
            array_shift($users); //去除本身数据
            return $users;
        }
        return $this->getParentsByUserId($userInfo['pid'], $users);
    }

    public function getTeamList($user_id,$return_arr = []){
        $child_list = User::where('pid',$user_id)->get();
        if($child_list){
            foreach ($child_list as $val){
                if($val['id']){
                    $return_arr[] = $val['id'];
                    $return_arr =  self::getTeamList($val['id'],$return_arr);
                }

            }
        }
        return $return_arr;
    }
}
